package service

import (
	"context"
	"strconv"
	"time"

	"gitee.com/jiebaiyou/formflow/model"
)

// profileService 实现了model.ProfileService接口, 提供用户资料相关的业务逻辑
type profileService struct {
	userRepository model.UserRepository
	contextTimeout time.Duration
}

// NewProfileService 创建并返回一个实现了model.ProfileService接口的实例NewProfileService
func NewProfileService(userRepository model.UserRepository, timeout time.Duration) model.ProfileService {
	return &profileService{
		userRepository: userRepository,
		contextTimeout: timeout,
	}
}

// GetProfileByID 根据用户ID获取用户资料
func (pu *profileService) GetProfileByID(c context.Context, userID string) (*model.Profile, error) {
	ctx, cancel := context.WithTimeout(c, pu.contextTimeout)
	defer cancel()

	// 将字符串ID转换为uint类型
	id, err := strconv.ParseUint(userID, 10, 32)
	if err != nil {
		return nil, err
	}

	user, err := pu.userRepository.GetByID(ctx, uint(id))
	if err != nil {
		return nil, err
	}

	return &model.Profile{Name: user.Name, Email: user.Email}, nil
}
